package com.cy.singleton;
/**
 * <br>@class: LazySingleton
 * <br>@description: 懒汉单例
 * <br>@author: cy
 * <br>@date: 2020-03-28 22:09:39
 * <br>@version: 1.0
 * <br>
 * <br>@since: (版本) 作者 时间 注释
 */
public class LazySingleton {
    private volatile static LazySingleton lazySingleton = null;

    private LazySingleton() {
        //懒汉模式
    }

    public static LazySingleton getInstance() {
        if (lazySingleton == null) {
            synchronized (LazySingleton.class) {
                if (lazySingleton == null) {
                    lazySingleton = new LazySingleton();
                    //字节码层
                }
            }
        }
        return lazySingleton;
    }

    public static void main(String[] args) {
        System.out.println("Test");
        for (int i = 0; i < 10; i++) {
            new Thread(new Test()).start();
        }
        new Thread(() -> {
            LazySingleton obj = LazySingleton.getInstance();
            System.out.println("Yhoo" + obj);
        }).start();
    }
}

class Test implements Runnable {

    @Override
    public void run() {
        LazySingleton obj = LazySingleton.getInstance();
        System.out.println(obj);
    }
}